package org.netEase.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.netEase.dao.shopCardDao;
import org.netEase.entity.shoppingCard;
import org.netEase.util.BaseDao;
import org.omg.CORBA.PUBLIC_MEMBER;


public class shopCardDaoImpl extends BaseDao implements shopCardDao {

	@Override
	public int getbyDate(String carddate) {
		int count = 0;
		String sql="select count(*) s from shopping_cart WHERE cart_date LIKE concat(?,'%')";
		ResultSet rs = super.executeQuery(sql, carddate);
		try {
			if(rs.next()) {
				count = rs.getInt("s");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}

	@Override
	public int addshopCard(shoppingCard shoppingCard) {
		String sql="INSERT INTO shopping_cart(cart_img,cart_name,cart_price,cart_num,cart_total,Hid,cart_typeg,cart_spid)VALUES(?,?,?,?,?,?,?,?)";
		return super.executeUpdate(sql, shoppingCard.getCartimg(),shoppingCard.getCartname(),shoppingCard.getCartprice(),shoppingCard.getCartnum(),shoppingCard.getCarttotal(),shoppingCard.getHid(),shoppingCard.getCart_typeg(),shoppingCard.getCart_spid());
	}

	@Override
	public int count(int sid) {
		int spcunt=0;
		String sql = "SELECT COUNT(*) s FROM shopping_cart WHERE cart_spid = ?";
		ResultSet rs = super.executeQuery(sql, sid);
		try {
			if (rs.next()) {
				spcunt = rs.getInt("rs");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return spcunt;
	}

	@Override
	public int updcart_num(int num,int sid) {
		String sql = "UPDATE shopping_cart SET cart_num =cart_num+? WHERE cart_spid = ?";
		return super.executeUpdate(sql,num, sid);
	}

	@Override
	public List<shoppingCard> getlist(int hid) {
		String sql = "select * from shopping_cart where 1=1 and hid = ?";
		List<shoppingCard> list = new ArrayList<>();
		ResultSet rs = super.executeQuery(sql, hid);
		try {
			while (rs.next()) {
				shoppingCard shoppingCard = new shoppingCard();
				shoppingCard.setCartid(rs.getInt("cart_id"));
				shoppingCard.setCart_spid(rs.getInt("cart_spid"));
				shoppingCard.setCart_typeg(rs.getString("cart_typeg"));
				shoppingCard.setCartimg(rs.getString("cart_img"));
				shoppingCard.setCartname(rs.getString("cart_name"));
				shoppingCard.setCartnum(rs.getInt("cart_num"));
				shoppingCard.setCartprice(rs.getDouble("cart_price"));
				shoppingCard.setCarttotal(rs.getDouble("cart_total"));
				shoppingCard.setHid(rs.getInt("hid"));
				
				
				list.add(shoppingCard);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	public List<shoppingCard> getlists(int hid,int hhid) {
		String sql = "select * from shopping_cart where 1=1 and hid = ?";
		List<shoppingCard> list = new ArrayList<>();
		List<Object> condition = new ArrayList<>();
		if (hhid !=0 && hhid>0) {
			sql+=" or hid = ?";
			condition.add(hhid);
		}
		condition.add(hid);
		ResultSet rs = super.executeQuery(sql,condition.toArray());
		try {
			while (rs.next()) {
				shoppingCard shoppingCard = new shoppingCard();
				shoppingCard.setCartid(rs.getInt("cart_id"));
				shoppingCard.setCart_spid(rs.getInt("cart_spid"));
				shoppingCard.setCart_typeg(rs.getString("cart_typeg"));
				shoppingCard.setCartimg(rs.getString("cart_img"));
				shoppingCard.setCartname(rs.getString("cart_name"));
				shoppingCard.setCartnum(rs.getInt("cart_num"));
				shoppingCard.setCartprice(rs.getDouble("cart_price"));
				shoppingCard.setCarttotal(rs.getDouble("cart_total"));
				shoppingCard.setHid(rs.getInt("hid"));
				
				
				list.add(shoppingCard);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public int piliang(int[] condition) {
		List<Object> params = new ArrayList<>();
		String sql = "delete from shopping_cart where cart_Id in (";
				for (int i = 0; i < condition.length; i++) {
					sql+="?";
					if(i<condition.length-1) {
						sql+=",";
					}
					params.add(condition[i]);
				}
				sql+=")";
		return super.executeUpdate(sql, params.toArray());
	}

	@Override
	public List<shoppingCard> getlistbycart_id(int [] cart_id) {
		List<Object> condition = new ArrayList<>();
		List<shoppingCard> list=  new ArrayList<>();
		String sql = "select * from shopping_cart where cart_Id in (";
		for (int i = 0; i < cart_id.length; i++) {
			sql+="?";
			condition.add(cart_id[i]);
			if (i<cart_id.length-1) {
				sql+=",";
			}
		}
		sql+=")";
		ResultSet rs = super.executeQuery(sql, condition.toArray());
		try {
			while (rs.next()) {
				shoppingCard shoppingCard = new shoppingCard();
				shoppingCard.setCartid(rs.getInt("cart_id"));
				shoppingCard.setCart_spid(rs.getInt("cart_spid"));
				shoppingCard.setCart_typeg(rs.getString("cart_typeg"));
				shoppingCard.setCartimg(rs.getString("cart_img"));
				shoppingCard.setCartname(rs.getString("cart_name"));
				shoppingCard.setCartnum(rs.getInt("cart_num"));
				shoppingCard.setCartprice(rs.getDouble("cart_price"));
				shoppingCard.setCarttotal(rs.getDouble("cart_total"));
				shoppingCard.setHid(rs.getInt("hid"));
				
				
				list.add(shoppingCard);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	/*public static void main(String[] args) {
		shopCardDao shopCardDao = new shopCardDaoImpl();
		List<shoppingCard> list=shopCardDao.getlists(0, 2);
		for (shoppingCard shoppingCard : list) {
			System.out.println(shoppingCard);
		}
		int [] a = {11,12};
		List<shoppingCard> list =shopCardDao.getlistbycart_id(a);
		for (shoppingCard shoppingCard : list) {
			System.out.println(shoppingCard);
		}
		
	}*/
	
}
